/**
 * Copyright (c) 2020 Coder League
 * All rights reserved.
 *
 * File：DistributionAreaDaoImpl.java
 * History:
 *         2020年12月27日: Initially created, Chrise.
 */
package club.coderleague.cfss.dao.impl;

import java.util.List;
import java.util.Map;

import club.coderleague.cfss.dao.extension.DistributionAreaDaoExtension;
import club.coderleague.cfss.domain.entities.Distributionareas;
import club.coderleague.cfss.domain.enums.EntityState;
import club.coderleague.data.jpa.id.support.SnowflakeV4;
import club.coderleague.data.jpa.repository.support.AbstractDataRepositoryExtension;

/**
 * 配送区域数据访问对象实现。
 * @author Chrise
 */
public class DistributionAreaDaoImpl extends AbstractDataRepositoryExtension<Distributionareas, Long, SnowflakeV4> implements DistributionAreaDaoExtension {
	/**
	 * @see club.coderleague.cfss.dao.extension.DistributionAreaDaoExtension#queryExistsDistributionArea(long, long, long, long, long)
	 */
	@Override
	public Distributionareas queryExistsDistributionArea(long supplier, long province, long city, long district, long street) {
		String jpql = "from Distributionareas where supplierid = ?0 and provinceid = ?1 and cityid = ?2 and districtid = ?3 and streetid = ?4";
		return super.queryEntity(jpql, supplier, province, city, district, street);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.DistributionAreaDaoExtension#queryDistributionArea(long)
	 */
	@Override
	public List<Map<String, Object>> queryDistributionArea(long entityid) {
		String sql = "select concat(p.areaname, ' ', c.areaname, ' ', d.areaname, ' ', s.areaname) as areaname from Distributionareas da " 
			+ "left join Areas p on da.provinceid = p.entityid " 
			+ "left join Areas c on da.cityid = c.entityid " 
			+ "left join Areas d on da.districtid = d.entityid " 
			+ "left join Areas s on da.streetid = s.entityid " 
			+ "where da.supplierid = ?0 and da.entitystate = ?1";
		return super.queryMapListBySql(sql, entityid, EntityState.VALID.getValue());
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.DistributionAreaDaoExtension#queryDistributionAreaData(long)
	 */
	@Override
	public List<Map<String, Object>> queryDistributionAreaData(long supplier) {
		String sql = "select da.entityid, da.provinceid, pn.areaname as pareaname, da.cityid, cn.areaname as careaname, " 
			+ "da.districtid, dn.areaname as dareaname, da.streetid, sn.areaname as sareaname " 
			+ "from Distributionareas da, Areas pn, Areas cn, Areas dn, Areas sn " 
			+ "where da.provinceid = pn.entityid and da.cityid = cn.entityid and da.districtid = dn.entityid and da.streetid = sn.entityid " 
			+ "and da.supplierid = ?0 and da.entitystate = ?1";
		return super.queryMapListBySql(sql, supplier, EntityState.VALID.getValue());
	}
}
